/**
* 生成日期选择器
* @param year		年
* @param month		月
* @param obj		文本框对象
* @param date		当前日期
*/
function calendar_getcalenderHtml(year,month,obj)
{	
	//创建日历对象
	var e=obj;
	var t=e.offsetTop;		//文本框横坐标  
	var l=e.offsetLeft; 	//文本框纵坐标
	var h=e.offsetHeight;
	var w=e.offsetWidth;
	while(e=e.offsetParent) 
	{  
		t+=e.offsetTop;  
		l+=e.offsetLeft;  
	} 
	var dq_calendar=document.createElement("calendar");	
	dq_calendar.className="dq_calendar";
	dq_calendar.targetObj=obj;		//目标框架
	l=l+w;
	dq_calendar.style.left=l+"px";
	dq_calendar.style.top=t+"px";
	document.getElementsByTagName('body')[0].appendChild(dq_calendar);
	
	date=calendar_getThisDateStr(obj);
	calendar_createMonth(year,month,dq_calendar,date);
}

/**
* 创建每月日期排列
* @param year		年
* @param month		月
* @param obj		日历对象
* @param date		当前日期
*/
function calendar_createMonth(year,month,obj,date)
{
	//标题对象
	var calendar_titlediv=document.createElement("div");
	calendar_titlediv.className="calendar_title";
	var strHTML='';
	strHTML+='<div class="calendar_titleleft" onclick="calendar_YearOffset(-1,this,'+year+','+month+',\''+date+'\')">&lt;&lt;</div>';
	strHTML+='<div class="calendar_titleleft" onclick="calendar_MonthOffset(-1,this,'+year+','+month+',\''+date+'\')">&lt;</div>';
	strHTML+='<div class="calendar_titleright" onclick="calendar_YearOffset(1,this,'+year+','+month+',\''+date+'\')">&gt;&gt;</div>';
	strHTML+='<div class="calendar_titleright" onclick="calendar_MonthOffset(1,this,'+year+','+month+',\''+date+'\')">&gt;</div>';
	strHTML+='<div class="calendar_titlecenter">'+year+'年'+month+'月</div>';
	calendar_titlediv.innerHTML=strHTML;
	obj.appendChild(calendar_titlediv);
	
	calendar_date=document.createElement("div");
	calendar_date.className="calendar_date";
	var aryMonthDays=[31,28,31,30,31,30,31,31,30,31,30,31];
	if (year%4==0){aryMonthDays[1]=29;}
	var MonthDays=aryMonthDays[month-1];
	
	var Month_start=calendar_getThisDayWeek(year,month,1);
	var calendar_date_html='';
	calendar_date_html+='<a class="calendar_week">日</a>';
	calendar_date_html+='<a class="calendar_week">一</a>';
	calendar_date_html+='<a class="calendar_week">二</a>';
	calendar_date_html+='<a class="calendar_week">三</a>';
	calendar_date_html+='<a class="calendar_week">四</a>';
	calendar_date_html+='<a class="calendar_week">五</a>';
	calendar_date_html+='<a class="calendar_week">六</a>';
	for(var i=0;i<Month_start;i++)
	{
		calendar_date_html+='<a class="calendar_other">&nbsp;</a>';
	}
	for(var i=1;i<=MonthDays;i++)
	{
		var thisdate=year+"-"+month+"-"+i;
		if(thisdate==date)
		{
			calendar_date_html+='<a href="javascript:void(0)" class="calendar_thisday" title="'+thisdate+'" onclick="selectdate(this)">'+i+'</a>';
		}
		else
		{
			calendar_date_html+='<a href="javascript:void(0)" class="calendar_other" title="'+thisdate+'" onclick="selectdate(this)">'+i+'</a>';
		}
	}	
	calendar_date.innerHTML=calendar_date_html;
	obj.appendChild(calendar_date);
	
	calendar_close=document.createElement("div");
	calendar_close.className="calendar_close";
	calendar_close.innerHTML='<a href="javascript:void(0)" title="关闭">×</a>';
	calendar_close.onclick=function(){calendar_closefunc(calendar_close.parentNode);}
	obj.appendChild(calendar_close);
	
}
/**
* 月偏移 
*/
function calendar_MonthOffset(offset,obj,year,month,date)
{
	month=month+offset;
	if(month<=0)
	{
		month=12;
		year--;
	}
	if(month>12)
	{
		month=1;
		year++;
	}
	if(year>9999 || year<1970)
	{
		alert("日期超出正常范围");
		return false;
	}
	var thisObj=obj.parentNode.parentNode;
	calendar_deleteAllChild(thisObj);
	calendar_createMonth(year,month,thisObj,date);
}

/**
* 年偏移
*/
function calendar_YearOffset(offset,obj,year,month,date)
{
	year=year+offset;
	if(year>9999 || year<1970)
	{
		alert("日期超出正常范围");
		return false;
	}
	var thisObj=obj.parentNode.parentNode;
	calendar_deleteAllChild(thisObj);
	calendar_createMonth(year,month,thisObj,date);
}


//删除所有子对象
function calendar_deleteAllChild(o)
{
	while(o.hasChildNodes())
	{
		o.removeChild(o.lastChild);
	}
}

/**
* @param obj 文本框对象
* @return object
*/
function calendar_getThisDate(obj)
{
	var date;
	if(obj.value=='' || obj.value=='YYYY-MM-DD')
	{
		date=new Date();
		year=date.getFullYear();
		month=date.getMonth()+1;
		day=date.getDate();
	}
	else
	{
		var aryDate=obj.value.split("-");
		year=aryDate[0];
		month=aryDate[1]-1;
		day=aryDate[2];
		date=new Date(year,month,day);
	}
	return date;
}

function calendar_getThisDayWeek(year,month,day)
{
	var date;
	month=month-1;
	if(month<0)
	{
		month=11;
	}
	date=new Date(year,month,day);
	return date.getDay();
}
/**
* @param obj 文本框对象
* @return object
*/
function calendar_getThisDateStr(obj)
{
	var date;
	if(obj.value=='' || obj.value=='YYYY-MM-DD')
	{
		date=new Date();
		year=date.getFullYear();
		month=date.getMonth()+1;
		day=date.getDate();
	}
	else
	{
		var aryDate=obj.value.split("-");
		year=aryDate[0];
		month=aryDate[1];
		day=aryDate[2];
		date=new Date(year,month,day);
	}
	ss=year+"-"+month+"-"+day;
	return ss;
}



/**
* 选择日期
* @param obj	日期对象
*/
function selectdate(obj)
{
	obj.parentNode.parentNode.targetObj.value=obj.title;
	
	if(obj.parentNode.parentNode.targetObj.value==obj.title)
	{
		calendar_closefunc(obj.parentNode.parentNode);
	}
}


//关闭日历
function calendar_closefunc(obj)
{
	obj.parentNode.removeChild(obj);
	setTimeout("calendar_closeAll()",10);
}

/**
* 关闭所有日期选择器
*/
function calendar_closeAll()
{
	var objs=document.getElementsByTagName("calendar");
	var objs_len=objs.length;
	for(var i=0; i<objs_len; i++)
	{
		objs[i].parentNode.removeChild(objs[i]);
	}
}

/**
* 显示日期选择器
* @param obj	文本框对象
* @return Void
*/
function DQ_setDate(obj)
{
	calendar_closeAll();	
	document.body.onfocus=function anonymous(){calendar_closeAll()};
	var objDate=calendar_getThisDate(obj);
	var year=objDate.getFullYear();
	var month=objDate.getMonth()+1;
	calendar_getcalenderHtml(year,month,obj);
}